#include "digital_display.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_gpio.h"

#define select_digital_port GPIOB
#define DIGITAL_SELECT_DIS1 GPIO_PIN_0
#define DIGITAL_SELECT_DIS2 GPIO_PIN_5
#define DIGITAL_SELECT_DIS3 GPIO_PIN_6
#define DIGITAL_SELECT_DIS4 GPIO_PIN_7


#define digital_data_port GPIOA
#define DIGITAL_DATA GPIO_PIN_5
#define DIGITAL_SHIFT_CLK GPIO_PIN_6
#define DIGITAL_LATCH_CLK GPIO_PIN_7
//函数简介：选择一个数码管显示数字
//参数：select:选择的数码管编号，num:要显示的数字
//返回值：无
void single_digit_display(unsigned char select,unsigned char num)
{
    unsigned char d[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
    //上面数组每个数组的数最低位都变为高位，第二低位变为第二位，以此类推
    unsigned char c[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
     //写74HC595移位寄存器
    for(int i=0;i<8;i++)
    {
        HAL_GPIO_WritePin(digital_data_port,DIGITAL_DATA,(c[num]>>i)&0x01);
        HAL_GPIO_WritePin(digital_data_port,DIGITAL_SHIFT_CLK,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(digital_data_port,DIGITAL_SHIFT_CLK,GPIO_PIN_SET);
    }
    //写74HC595锁存寄存器
    HAL_GPIO_WritePin(digital_data_port,DIGITAL_LATCH_CLK,GPIO_PIN_RESET);
    HAL_GPIO_WritePin(digital_data_port,DIGITAL_LATCH_CLK,GPIO_PIN_SET);
    switch(select)
    {
        case 1:
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS1,GPIO_PIN_SET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS2,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS3,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS4,GPIO_PIN_RESET);
            break;
        case 2:
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS1,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS2,GPIO_PIN_SET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS3,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS4,GPIO_PIN_RESET);
            break;
        case 3:
            HAL_GPIO_WritePin(select_digital_port, DIGITAL_SELECT_DIS1 ,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS2,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS3,GPIO_PIN_SET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS4,GPIO_PIN_RESET);
            break;
        case 4:
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS1,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS2,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS3,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS4,GPIO_PIN_SET);
            break;
        default:
          //关4所有数码管
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS1,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS2,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS3,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(select_digital_port,DIGITAL_SELECT_DIS4,GPIO_PIN_RESET);
            for(int i=0;i<8;i++)
            {
                HAL_GPIO_WritePin(digital_data_port,DIGITAL_DATA,0);
                HAL_GPIO_WritePin(digital_data_port,DIGITAL_SHIFT_CLK,GPIO_PIN_RESET);
                HAL_GPIO_WritePin(digital_data_port,DIGITAL_SHIFT_CLK,GPIO_PIN_SET);
            }
            //写74HC595锁存寄存器
            HAL_GPIO_WritePin(digital_data_port,DIGITAL_LATCH_CLK,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(digital_data_port,DIGITAL_LATCH_CLK,GPIO_PIN_SET);
            break;
    }
   
}
//函数简介：数码管显示数字，最多四位
//参数：num:要显示的数字
//返回值：无
void digital_display(int num)
{
    unsigned char weishu=0;
    //判断num的位数
    if(num>9999)
    {
        num=9999;
    }
    if(num<0)
    {
        num=0;
    }
    //num除十等于0，就表示num是个位数
    if(num/10==0)
    {
        weishu = 1;
    }
    else if(num/100==0)
    {
        weishu = 2;
    }
    else if(num/1000==0)
    {
        weishu = 3;
    }
    else
    {
        weishu = 4;
    }
    //把个位放在第四个数码管显示
    //把十位放在第三个数码管显示
    //把百位放在第二个数码管显示
    //把千位放在第一个数码管显示
    switch (weishu)
    {
        case 1:
            single_digit_display(4,num);
            HAL_Delay(1);
            break;
        case 2:
            single_digit_display(3,num/10);
            HAL_Delay(1);
            // single_digit_display(5,1);
            single_digit_display(4,num%10);
            HAL_Delay(1);
            break;
        case 3:
            single_digit_display(2,num/100);
            // single_digit_display(5,1);
            HAL_Delay(1);
            single_digit_display(3,(num/10)%10);
            HAL_Delay(1);
            // single_digit_display(5,1);
            single_digit_display(4,num%10);
            HAL_Delay(1);
            break;
        case 4:
            single_digit_display(1,num/1000);
            HAL_Delay(1);
            single_digit_display(5,1);
            single_digit_display(2,(num/100)%10);
            HAL_Delay(1);
            single_digit_display(5,1);
            single_digit_display(3,(num/10)%10);
            HAL_Delay(1);
            single_digit_display(5,1);
            single_digit_display(4,num%10);
            HAL_Delay(1);
            single_digit_display(5,1);
            break;
        default:
            single_digit_display(5,1);
            single_digit_display(5,1);
            single_digit_display(5,1);
            single_digit_display(5,1);
            break;
    }
}